// miniprogram/pages/payment/payment.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item_info: {
      name: '向日葵之歌-向日葵三枝,戴安娜粉玫瑰三枝',
      price: 198.00,
      imgs: ['../../images/it1.jpg', '../../images/it2.jpg', '../../images/it3.jpg', '../../images/it4.jpg', ]
    },
    invoice_info: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var binfo = wx.getStorageSync('buyinfo');
    var user_address = app.globalData.user_address;
    var total = 0;
    for (let i = 0; i < binfo.length; i++) {
      total += parseFloat(binfo[i].cart_buy_number * binfo[i].cart_item_price);
    }
    this.setData({
      binfo: binfo,
      user_address: user_address,
      total: total.toFixed(2)
    })
  },
  getInvoiceInfo() {
    let invoice_info = {};
    let _this = this;
    wx.chooseInvoiceTitle({
      success(res) {
        console.log(res)
        for (let i in res) {
          if (i != 'errMsg') {
            invoice_info[i] = res[i]
          }
        }
        console.log(invoice_info)
        _this.setData({
          invoice_info: invoice_info
        })
      }
    })
  },
  getLeaveMessage(e) {
    let val = e.detail.value;
    this.setData({
      leave_message: val
    })
  },
  wxPayment() {

  },
  submitOrder() {
    let binfo = this.data.binfo;
    let cart = this.data.binfo;
    let buy = []
    for (let i = 0; i < binfo.length; i++) {
      let buy_info = {};
      for (let j in binfo[i]) {
        if (j != '_id' && j != '_openid') {
          buy_info[j] = binfo[i][j]
        }
      }
      buy.push(buy_info)
    }
    binfo = buy;
    console.log(binfo);
    let invoice_info = this.data.invoice_info;
    let user_address = this.data.user_address;
    let leave_message = this.data.leave_message;
    let total = this.data.total;
    let status = 1;
    let _this = this;
    let address_data = {};
    //let total = (binfo.goods_price * binfo.buy_number).toFixed(2);
    let db = wx.cloud.database();
    let time = new Date();
    let yy = time.getFullYear();
    let st = time.getTime(),
      mm = time.getMonth() + 1 > 9 ? time.getMonth() + 1 : '0' + (time.getMonth() + 1),
      dd = time.getDate() + 1 > 9 ? time.getDate() : '0' + time.getDate(),
      hh = time.getHours() > 9 ? time.getHours() : '0' + time.getHours(),
      min = time.getMinutes() > 9 ? time.getMinutes() : '0' + time.getMinutes(),
      sec = time.getSeconds() > 9 ? time.getSeconds() : '0' + time.getSeconds();
    let create_time = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + min + ':' + sec;
    let order_id = st + '' + parseInt(Math.random() * 100000);
    if (!user_address.uname) {
      wx.showToast({
        title: '请选择地址',
      })
      return false;
    } else {
      for (let i in user_address) {
        if (i != '_id' && i != '_openid') {
          address_data[i] = user_address[i]
        }
      }
    }
    let data = {
      order_item_info: binfo,
      order_user_info: address_data,
      order_shipping_method: '快递物流',
      order_pay_method: '微信支付',
      order_bill_info: invoice_info ? invoice_info : '无',
      order_leave_message: leave_message ? leave_message : '',
      order_ticket_list: [],
      order_item_price: total,
      order_fare: 0,
      order_benifit: 0,
      order_vip_discount: 0,
      order_total_money: total,
      order_status: status,
      order_num: binfo.buy_number,
      order_create_time: create_time,
      order_id: order_id
    }
    console.log(data);
    db.collection('tb_order_list').add({
      data: data,
      success: res => {
        console.log(res)
        if (res.errMsg == 'collection.add:ok') {
          for (let i = 0; i < cart.length; i++) {
            console.log(cart[i]._id);
            db.collection('tb_cart_list').doc(cart[i]._id).remove();
          }
        }
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  goAddress() {
    let _this = this;
    wx.navigateTo({
      url: '../applications/address/address',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})